package com.leo.collisions;

import com.almasb.fxgl.core.math.FXGLMath;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.physics.CollisionHandler;
import com.leo.common.Config;
import com.leo.common.GameType;
import javafx.geometry.Rectangle2D;

/**
 * 子弹和敌人的碰撞检测
 */
public class BulletEnemyHandler extends CollisionHandler {

    public BulletEnemyHandler() {
        super(GameType.BULLET, GameType.ENEMY);
    }

    @Override
    protected void onCollisionBegin(Entity a, Entity b) {
        FXGL.spawn("explode", b.getCenter().subtract(50 / 2.0, 50 / 2.0));
        a.removeFromWorld();
        b.removeFromWorld();
        // 摧毁敌人数量加1
        FXGL.inc("destroyedEnemyAmount", 1);
        // 子弹和敌人相碰撞时产生一个道具
        if (FXGLMath.randomBoolean()) {
            FXGL.spawn("item",
                    FXGLMath.randomPoint(
                            new Rectangle2D(Config.CELL_SIZE, Config.CELL_SIZE,
                                    27 * Config.CELL_SIZE - 30, 27 * Config.CELL_SIZE - 28)));
        }
    }
}
